#!/usr/local/bin/python3
# *_* coding: UTF-8 *_*
# @IDE: PyCharm
# @Version: Python3.97
# @Author: Kendrick.Kc
# @Email: 509556985@qq.com
# @File: signals.py
# @Inst: 信号监听
# @Time: 2023/5/13 23:30
# -----


from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.cache import cache
from ROOT import settings
from basic.models import EmailModel


@receiver(post_save, sender=EmailModel)
def basic_signals_email(sender, instance, created, **kwargs):
    """
    当EmailModel有数据变更时，更新全局日志收件人变量
    :param sender:
    :param instance:
    :param created:
    :param kwargs:
    :return:
    """
    settings.LOGGING['handlers']['email_warning']['toaddrs'] = [instance.address]
    settings.LOGGING['handlers']['email_error']['toaddrs'] = [instance.address]
    cache.set("logging", settings.LOGGING)  # 将日志模块配置保存到内存缓存
    cache.set("email", [instance.address])  # 将日志模块配置保存到内存缓存
